<template>
  <div class="settings-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">Your Settings</h1>

          <form>
            <fieldset>
              <fieldset class="form-group">
                <input
                  v-model="image"
                  class="form-control"
                  type="text"
                  placeholder="URL of profile picture"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="username"
                  required
                  class="form-control form-control-lg"
                  type="text"
                  placeholder="Your Name"
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  v-model="bio"
                  class="form-control form-control-lg"
                  rows="8"
                  placeholder="Short bio about you"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="email"
                  required
                  class="form-control form-control-lg"
                  type="text"
                  placeholder="Email"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="password"
                  required
                  class="form-control form-control-lg"
                  type="password"
                  placeholder="Password"
                  minlength="8"
                />
              </fieldset>
              <button
                @click="onUpdate"
                :disabled="!formValidate || updateDisabled"
                :class="{ disabled: !formValidate || updateDisabled }"
                class="btn btn-lg btn-primary pull-xs-right"
              >Update Settings</button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { updateUser } from "@/api/user";

export default {
  middleware: "authenticated",
  computed: {
    ...mapState(["user"]),
    formValidate() {
      return this.username && this.password && this.email;
    },
  },
  data() {
    return {
      email: "",
      image: "",
      bio: "",
      username: "",
      password: "",
      updateDisabled: false,
    };
  },
  mounted() {
    const { email, image, bio, username } = this.user;
    this.email = email;
    this.image = image;
    this.bio = bio;
    this.username = username;
  },
  methods: {
    async onUpdate() {
      this.updateDisabled = true;
      await updateUser({
        email: this.email,
        username: this.username,
        password: this.password,
        image: this.image,
        bio: this.bio,
      });
      this.updateDisabled = false;
    },
  },
};
</script>

<style>
</style>